//
//  WSMusicPlayerViewController.m
//  musicPalyer
//
//  Created by ws on 2019/2/14.
//  Copyright © 2019年 王松. All rights reserved.
//

#import "WSMusicPlayerViewController.h"
#import "WSPlayerToolBar.h"
#import "WSMusic.h"
#import "MJExtension.h"
#import "WSMusicTableViewCell.h"
#import "WSMusicTool.h"
@interface WSMusicPlayerViewController ()<UITableViewDataSource,UITableViewDelegate,WSPlayToolBarDelegate,AVAudioPlayerDelegate>
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property(nonatomic,strong)NSArray *musics;//音乐数据
@property(nonatomic,assign)NSInteger musicIndex;//当前播放音乐的索引
@property(nonatomic,strong)WSPlayerToolBar *toolBar;//播放的工具条
@end

@implementation WSMusicPlayerViewController
#pragma mark 懒加载音乐数据
-(NSArray *)musics{
    if (!_musics) {
        _musics = [WSMusic mj_objectArrayWithFilename:@"songs.plist"];
    }
    return _musics;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //1.添加播放的工具条
    self.toolBar = [WSPlayerToolBar playerToolBar];
    //设置toolBar的尺寸
    self.toolBar.bounds = self.bottomView.bounds;
    //设置代理
    self.toolBar.delegate = self;
    [self.bottomView addSubview:self.toolBar];
    //2.设置表格的透明度
    self.myTableView.backgroundColor = [UIColor clearColor];
    NSLog(@"播放的下标 == %ld",self.musicIndex);
//    //3.初始化播放的音乐
//    self.toolBar.playingMusicModel = self.musics[self.musicIndex];
//    //4.准备播放
//    [[WSMusicTool ws_musicTool] ws_prepareToPlayWithMusic:self.musics[self.musicIndex]];
    
    [self ws_playMusicAction];
    
}
#pragma mark 表格数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.musics.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WSMusicTableViewCell *cell = [WSMusicTableViewCell musicTableView:tableView];
    //设置数据
    WSMusic *music = self.musics[indexPath.row];
    cell.musicModel = music;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    WSMusic *music = self.musics[indexPath.row];
//    self.toolBar.playingMusicModel = music;
    //更改索引
    self.musicIndex = indexPath.row;
    //播放音乐
    [self ws_playMusicAction];
}

#pragma mark WSPlayToolBarDelegate 播放工具条的代理
-(void)ws_playToolBar:(WSPlayerToolBar *)toolBar btnClickWtihType:(BtnType)btnType{
    //实现播放  把播放的操作放在一个工具类里面
    switch (btnType) {
        case BtnTYpePlay:
            //播放
            NSLog(@"播放");
            [[WSMusicTool ws_musicTool] ws_play];
            break;
        case BtnTypePause:
            //暂停
            NSLog(@"暂停");
            [[WSMusicTool ws_musicTool] ws_pause];
            break;
        case BtnTypePrevious:
            //上一首
            NSLog(@"上一首");
            [self ws_playPreviousAction];
            break;
        case BtnTypeNext:
            //下一首
            NSLog(@"下一首");
            [self ws_playNextAction];
            break;
            
        default:
            break;
    }
}
#pragma mark 播放上一首
-(void)ws_playPreviousAction{
    //1.更改播放的索引
    self.musicIndex = self.musicIndex == 0 ? self.musics.count- 1 : --self.musicIndex;
    [self ws_playMusicAction];
}


#pragma mark 播放下一首
-(void)ws_playNextAction{
    //1.更改播放的索引
    self.musicIndex = self.musicIndex == self.musics.count- 1 ? 0 : ++self.musicIndex;
    [self ws_playMusicAction];
}

#pragma mark
-(void)ws_playMusicAction{
    //2.从新初始化一个播放器
    [[WSMusicTool ws_musicTool] ws_prepareToPlayWithMusic:self.musics[self.musicIndex]];
    //设置player的代理 -- 用来监听是否播放完毕 来继续播放下一首
    [WSMusicTool ws_musicTool].ws_player.delegate = self;
    
    
    //3.更改播放器工具条的数据
    self.toolBar.playingMusicModel = self.musics[self.musicIndex];
    //4.播放
    if (self.toolBar.isPlaying) {
        [[WSMusicTool ws_musicTool] ws_play];
    }
}

#pragma mark 播放器的代理 -- 自动播放下一首
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    //自动播放下一首
    [self ws_playNextAction];
}
@end
